import { legacy_createStore as createStore } from 'redux'
import counterReducer from './reducers/counter'
import { decrement, increment } from './actions/counter'

// 参数：reducer
// 返回值：store 实例
const store = createStore(counterReducer)

// 输出当前的状态
console.log(store.getState()) // 10

/* // 通过 actionCreator 生成 action，简化代码
store.dispatch(increment(3))
console.log(store.getState()) // 13

store.dispatch(decrement(5))
console.log(store.getState()) // 8

store.dispatch(increment(2))
console.log(store.getState()) // 10 */

// 有没有办法监听派发的 action，监听到后执行对应的回调
// !subscribe 要放到 dispatch 的前面
const unSubscribe = store.subscribe(() => {
  // 每次 dispatch 了 action，state 更新，自动调用
  console.log(store.getState())
})
// 取消订阅/监听
// unSubscribe()

store.dispatch(increment(3))
store.dispatch(decrement(5))
store.dispatch(increment(2))
